// 锁 黄牛抢票
#include "mutex.hpp"
#include <unistd.h>
int tickets = 100;
class CC
{
public:
    CC(Mutex &mutex, string s) : _s(s),
                                 _mutex(mutex)
    {
    }
    void lock()
    {
        _mutex.lock();
    }
    void unlock()
    {
        _mutex.Unlock();
    }
    string name()
    {
        return _s;
    }

private:
    string _s;    // 客户的名字
    Mutex _mutex; // 锁
};

void *QiangPiao(void *args)
{
    CC *c = (CC *)args;
    cout<<"进入线程"<<endl;
    while (true)
    {
        {
            c->lock();
            if(c->name()=="客户2")
            {
                sleep(2);

            }
            else{
                sleep(1);
            }
            tickets--;
            cout<<c->name()<<"抢到票"<<endl;
            
            
            c->unlock();
            
        }
    }
}

int main()
{
    pthread_t c1, c2;
    Mutex mutex;
    CC cc1(mutex, "客户1");
    CC cc2(mutex, "客户2");
    cout<<"begin"<<endl;
    pthread_create(&c1, nullptr, QiangPiao, (void *)&cc1);
    pthread_create(&c2, nullptr, QiangPiao, (void *)&cc2);

    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);
    return 0;
}
